﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace DataCenter
{
    public static class RemoteDate
    {
        //查询指定脚本
        public static async Task<string> GetScript(string Api, string Token, int scriptId)
        {
            // 设置 API 地址和接口路径
            string apiUrl = Api;
            string apiPath = "/scriptbyid";

            // 设置 Authorization 头的值
            string token = Token;

            // 构建完整的 URL
            string fullUrl = $"{apiUrl}{apiPath}?script_id={scriptId}";

            // 创建 HttpClient 实例
            using (HttpClient httpClient = new HttpClient())
            {
                // 添加 Authorization 头
                httpClient.DefaultRequestHeaders.Add("Authorization", token);

                try
                {
                    // 发送 GET 请求
                    HttpResponseMessage response = await httpClient.GetAsync(fullUrl);

                    // 检查响应是否成功
                    if (response.IsSuccessStatusCode)
                    {
                        // 读取响应内容
                        string responseBody = await response.Content.ReadAsStringAsync();
                        return responseBody; // 返回响应内容
                    }
                    else
                    {
                        Console.WriteLine($"Error: {response.StatusCode} - {response.ReasonPhrase}");
                        return null; // 或者抛出异常或其他处理方式
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error: {ex.Message}");
                    return null; // 或者抛出异常或其他处理方式
                }
            }
        }
    }
}
